﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserControlSupport_DetailProduct : System.Web.UI.UserControl
{
    private DataTable dataTable;
    private string ProId;
    DropDownList ProSize, ProColor;
    ShoppingDataContext spdc;

    protected void Page_Load(object sender, EventArgs e)
    {
        ProId = Request["id"];
        if (!IsPostBack)
        {
            this.dataTable = new DataTable();
            this.dataTable.Columns.Add("ProImage");
            this.dataTable.Columns.Add("ProName");            
            this.dataTable.Columns.Add("ProPrice");
            this.dataTable.Columns.Add("ProSizes");
            this.dataTable.Columns.Add("ProColors");
            this.dataTable.Columns.Add("ProDes");
            this.dataTable.Columns.Add("ProDesId");
            this.dataTable.Columns.Add("instock");



            spdc = new ShoppingDataContext();

            PRODUCT product = spdc.PRODUCTs.Single(p => p.ProductID == ProId);
            product.countview = product.countview + 1;
            spdc.SubmitChanges();

            var Pro = from p in spdc.PRODUCTs
                      where p.ProductID == ProId
                      select p;


            DataRow row = dataTable.NewRow();
            row["ProImage"] = Pro.First().image;
            row["ProName"] = Pro.First().ProName;
            row["ProDes"] = Pro.First().Descript;

            var ProDetail = from q in spdc.PRODUCT_DEs
                            where q.ProductID == ProId
                            select q;
            
            dataTable.Rows.Add(row);
            _ProDetails.DataSource = dataTable;
            _ProDetails.DataBind();
        }
    }

    protected void SizeChange(object sender, EventArgs e)
    {
        ProSize = (DropDownList) sender;
        DataListItem dt = (DataListItem)ProSize.Parent;
        ProColor = (DropDownList) dt.FindControl("_ProColor");
        HiddenField hf = (HiddenField)dt.FindControl("_ProDesId");
        Label instk = (Label)dt.FindControl("instock");
        Label price = (Label)dt.FindControl("_ProPrice");
        Image img = (Image)dt.FindControl("_ProImage");

        spdc = new ShoppingDataContext();
        var ProColDetail = from q in spdc.PRODUCT_DEs
                           where q.ProductID == ProId && q.Size == ProSize.SelectedValue
                           select q.Color;

        ArrayList color = new ArrayList();
        foreach (var i in ProColDetail)
        {
            if (!color.Contains(i))
            {
                color.Add(i);
            }
        }
        ProColor.DataSource = color.ToArray();
        ProColor.DataBind();

        var ProDesDetail = from q in spdc.PRODUCT_DEs
                           where q.ProductID == ProId && 
                                 q.Size == ProSize.SelectedValue &&
                                 q.Color == ProColor.SelectedValue
                           select q;

        hf.Value = ProDesDetail.First().Product_DesID;
        instk.Text = ProDesDetail.First().number.ToString();
        price.Text = ProDesDetail.First().price.ToString();
        img.ImageUrl = ProDesDetail.First().image;
    }

    protected void ColorChange(object sender, EventArgs e)
    {
        ProColor = (DropDownList)sender;
        DataListItem dt = (DataListItem)ProColor.Parent;
        ProSize = (DropDownList)dt.FindControl("_ProSize");
        Label instk = (Label)dt.FindControl("instock");
        Label price = (Label)dt.FindControl("_ProPrice");
        Image img = (Image)dt.FindControl("_ProImage");

        spdc = new ShoppingDataContext();
        var ProDesDetail = from q in spdc.PRODUCT_DEs
                           where q.ProductID == ProId && 
                                    q.Size == ProSize.SelectedValue && 
                                    q.Color == ProColor.SelectedValue
                           select q;

        HiddenField hf = (HiddenField)dt.FindControl("_ProDesId");
        hf.Value = ProDesDetail.First().Product_DesID;
        instk.Text = ProDesDetail.First().number.ToString();
        price.Text = ProDesDetail.First().price.ToString();
        img.ImageUrl = ProDesDetail.First().image;
    }
    
    protected void DataBound(object sender, DataListItemEventArgs e)
    {
        ProSize = (DropDownList)(e.Item.FindControl("_ProSize"));
        ProColor = (DropDownList)(e.Item.FindControl("_ProColor"));
        HiddenField hf = (HiddenField)(e.Item.FindControl("_ProDesId"));
        Label instk = (Label)(e.Item.FindControl("instock"));
        Label price = (Label)(e.Item.FindControl("_ProPrice"));
        Image img = (Image)(e.Item.FindControl("_ProImage"));

        var ProSizeDetail = from q in spdc.PRODUCT_DEs
                        where q.ProductID == ProId
                        select q;
        
        ArrayList size = new ArrayList();
        foreach (var i in ProSizeDetail)
        {
            if (!size.Contains(i.Size))
            {
                size.Add(i.Size);
            }
        }
        ProSize.DataSource = size.ToArray();
        ProSize.DataBind();

        var ProColorDetail = from q in spdc.PRODUCT_DEs
                            where q.ProductID == ProId && q.Size == ProSize.SelectedValue
                            select q;
        ArrayList color = new ArrayList();
        foreach (var i in ProColorDetail)
        {
            if (!color.Contains(i.Color))
            {
                color.Add(i.Color);
            }
        }

        ProColor.DataSource = color.ToArray();
        ProColor.DataBind();

        var ProDesDetail = from q in spdc.PRODUCT_DEs
                             where q.ProductID == ProId && 
                                    q.Size == ProSize.SelectedValue && 
                                    q.Color == ProColor.SelectedValue
                             select q;
        if (ProDesDetail.Count() > 0)
        {
            hf.Value = ProDesDetail.First().Product_DesID;
            instk.Text = ProDesDetail.First().number.ToString();
            price.Text = ProDesDetail.First().price.ToString();
            img.ImageUrl = ProDesDetail.First().image;
        }
        else
        {
            instk.Text = "0";
        }
    }

    protected void BuyClick(object sender, ImageClickEventArgs e)
    {
        ImageButton Probuy = (ImageButton)sender;
        DataListItem dt = (DataListItem)Probuy.Parent;
        HiddenField ProId = (HiddenField)dt.FindControl("_ProDesId");
        TextBox Count = (TextBox)dt.FindControl("Count");
        Label instk = (Label)dt.FindControl("instock");
        if (int.Parse(instk.Text) < int.Parse(Count.Text))
        {
            error.Text = "Vượt quá số lượng";
            error.Visible = true;
            return;
        }
        ArrayList ProIdCard;
        ArrayList CountCard;
        if (Session["ProIdCard"] == null)
        {
            ProIdCard = new ArrayList();
            CountCard = new ArrayList();
        }
        else
        {
            ProIdCard = (ArrayList)Session["ProIdCard"];
            CountCard = (ArrayList)Session["CountCard"];
        }

        if (ProIdCard.Contains(ProId.Value))
        {
            CountCard[ProIdCard.IndexOf(ProId)] = Count.Text;
        }
        else
        {
            ProIdCard.Add(ProId.Value);
            CountCard.Add(Count.Text);
        }

        Session["ProIdCard"] = ProIdCard;
        Session["CountCard"] = CountCard;
    }
}
